package com.battery.system.service.battery.impl;

import java.util.Arrays;
import java.util.List;

import com.battery.common.enums.YesOrNoEnum;
import com.battery.system.domain.BO.ItemsBO;
import com.battery.system.domain.VO.ItemsVO;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.battery.system.mapper.ItemsMapper;
import com.battery.system.domain.Items;
import com.battery.system.service.battery.IItemsService;
import com.battery.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * 商品Service业务层处理
 *
 * @author battery
 * @date 2021-01-22
 */
@Service
public class ItemsServiceImpl extends BaseServiceImpl<Items> implements IItemsService {
    @Autowired
    private ItemsMapper itemsMapper;

    /**
     * 查询商品
     *
     * @param id 商品ID
     * @return 商品
     */
    @Override
    public Items selectItemsById(String id) {
        return itemsMapper.selectItemsById(id);
    }

    /**
     * 查询商品列表
     *
     * @param items 商品
     * @return 商品
     */
    @Override
    public List<Items> selectItemsList(Items items) {
        return itemsMapper.selectItemsList(items);
    }

    /**
     * 新增商品
     *
     * @param items 商品
     * @return 结果
     */
    @Override
    public int insertItems(Items items) {
        return itemsMapper.insertItems(items);
    }

    /**
     * 修改商品
     *
     * @param items 商品
     * @return 结果
     */
    @Override
    public int updateItems(Items items) {
        return itemsMapper.updateItems(items);
    }

    /**
     * 删除商品对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteItemsByIds(String ids) {
        return itemsMapper.deleteItemsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商品信息
     *
     * @param id 商品ID
     * @return 结果
     */
    @Override
    public int deleteItemsById(String id) {
        return itemsMapper.deleteItemsById(id);
    }

    /**
     * 设备申领-商品列表
     *
     * @param itemsBO
     * @return
     */
    @Override
    public List<ItemsVO> devApplyItemsList(ItemsBO itemsBO) {
        return itemsMapper.devApplyItemsList(itemsBO);
    }

    /**
     * @param itemName
     * @param purpose
     * @return
     */
    @Override
    public Items selectByNameAndPurpose(String itemName, Integer purpose) {
        Example example = new Example(Items.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", YesOrNoEnum.NO.getCode());
        criteria.andEqualTo("purpose", purpose);
        criteria.andEqualTo("itemName", itemName);
        return itemsMapper.selectOneByExample(example);
    }

    /**
     * @param devType
     * @param purpose
     * @return
     */
    @Override
    public Items selectByDevTypeAndPurpose(String devType, Integer purpose) {
        Example example = new Example(Items.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", YesOrNoEnum.NO.getCode());
        criteria.andEqualTo("purpose", purpose);
        criteria.andEqualTo("devType", devType);
        return itemsMapper.selectOneByExample(example);
    }


    /**
     * 上下架商品
     *
     * @param ids
     * @param on_off_status
     * @return
     */
    @Transactional
    @Override
    public int onOffItems(String ids, Integer on_off_status) {
        Example example = new Example(Items.class);
        Example.Criteria criteria = example.createCriteria();
        String[] idsArr = ids.split(",");
        criteria.andIn("id", Arrays.asList(idsArr));
        Items updateItems = new Items();
        updateItems.setOnOffStatus(on_off_status);

        return itemsMapper.updateByExampleSelective(updateItems, example);
    }

    /**
     * 在线采购
     *
     * @param itemsBO
     * @return
     */
    @Override
    public List<ItemsVO> purchaseItemsList(ItemsBO itemsBO) {
        return itemsMapper.purchaseItemsList(itemsBO);
    }

    @Override
    public List<ItemsVO> pointsExchangeItemsList(ItemsBO itemsBO) {
        return itemsMapper.pointsExchangeItemsList(itemsBO);
    }
}
